package com.bytedance.danmaku.render.engine.demo

import android.annotation.SuppressLint
import android.net.Uri
import android.os.Bundle
import android.view.ViewGroup
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import com.bytedance.danmaku.render.engine.DanmakuView
import com.bytedance.danmaku.render.engine.control.DanmakuController
import com.bytedance.danmaku.render.engine.data.DanmakuData
import com.bytedance.danmaku.render.engine.demo.view.CustomVideoView
import com.bytedance.danmaku.render.engine.demo.view.RatioRelativeLayout
import com.bytedance.danmaku.render.engine.render.draw.text.TextData
import com.bytedance.danmaku.render.engine.utils.LAYER_TYPE_SCROLL


class MainActivityVideoChange : AppCompatActivity() {
    private lateinit var mController: DanmakuController
    @SuppressLint("MissingInflatedId")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main_video_change)
        var parentView = findViewById<RatioRelativeLayout>(R.id.parent_view)
        val videoView = findViewById<CustomVideoView>(R.id.video_view)
        val danmakuView = findViewById<DanmakuView>(R.id.danmaku_view)
        videoView.setVideoURI(Uri.parse("android.resource://" + packageName + "/" + R.raw.video))
        videoView.start()
        findViewById<Button>(R.id.change).setOnClickListener {
            val layoutParams: ViewGroup.LayoutParams = parentView.getLayoutParams()
            layoutParams.width = 600 // 设置宽度为200像素
            parentView.setLayoutParams(layoutParams)
        }
        mController = danmakuView.controller
        mController.setData(buildDanmakuData())
        mController.start()
    }
    fun buildDanmakuData(): List<DanmakuData> {
        val contents = resources.getStringArray(R.array.danmaku_text_contents)
        val list = mutableListOf<TextData>()
        for (i in 0..99) {
            list.add(TextData().apply {
                text = contents.random()
                layerType = LAYER_TYPE_SCROLL
//                showAtTime = i * 600L +  (0..300L).random()
                showAtTime = i * 1000L
            })
        }
        return list
    }
}